﻿unit mp1;

{
public domain Simple, Minimalistic MPEG Layer 1 decoder - http://jonolick.com
(2014-26-1) Initial release.
Translation to Pascal: domasz & serbod

Basic usage:

var
  h: TMp1DecoderContext;
  BufOut: TSamplesArray;
  msIn, msOut: TMemoryStream;
  res: Integer;
begin
  jo_mp1_init(h);
  msOut := TFileStream.Create();
  msIn := TFileStream.Create();
  try
    msIn.ReadFromFile('sample.mp1');
    repeat
      res := jo_mp1_decode(h, ms.Memory, ms.Size, BufOut);
      if res > 0 then
        msOut.Write(BufOut, Length(BufOut) * SizeOf(BufOut[0]));
    until res < 0;
    msOut.SaveToFile('sample.raw');
  finally
    msIn.Free();
    msOut.Free();
  end;
end;

}

interface

uses SysUtils;

const
  MP1_ERR_SYNC_MARK  = -1;
  MP1_ERR_HEADER     = -2;
  MP1_ERR_BLOCK_SIZE = -3;
  MP1_ERR_DATA_SIZE  = -4;

type
  TSamplesArray = array of SmallInt;
  TMp1DecoderContext = record
    buf: array[0..1, 0..2*512-1] of Double; // Buffers for the lapped transform
    bufOffset: array[0..1] of Integer;
    kbps: Integer;      // Bit rate, KBit/s
    hz: Integer;        // Sample rate
    channels: Integer;  // 1-mono, 2-stereo
    BlockSize: Integer; // frame size in bytes
  end;

// Initialize decoder context
procedure jo_mp1_init(out h: TMp1DecoderContext);

// Decode one frame (384 samples) from input buffer to output samples array
// input, inputSize - input data
// output - array length set on decoding
// Result:
//   >0 - how many bytes decoded from input
//   -1 - sync marker not found
//   -2 - wrong header format
//   -3 - wrong block size
//   -4 - not enough input data
function jo_mp1_decode(var h: TMp1DecoderContext;
  const input: Pointer; inputSize: Integer;
  var output: TSamplesArray): Integer;

implementation

const
  //bitrateTbl1: array[0..15] of Integer = (0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,-1); // MPEG Version 1 (ISO/IEC 11172-3)
  bitrateTbl2: array[0..15] of Integer = (0,32,48,56, 64, 80, 96,112,128,144,160,176,192,224,256,-1); // MPEG Version 2 (ISO/IEC 13818-3)
  hzTbl: array[0..3] of Integer = ( 44100, 48000, 32000, 0 );

const // 1d arrays
  GMultTbl: array[0..63] of Double = (
    2.000000, 1.587401, 1.259921, 1.000000, 0.793701, 0.629961, 0.500000, 0.396850,
    0.314980, 0.250000, 0.198425, 0.157490, 0.125000, 0.099213, 0.078745, 0.062500,
    0.049606, 0.039373, 0.031250, 0.024803, 0.019686, 0.015625, 0.012402, 0.009843,
    0.007812, 0.006201, 0.004922, 0.003906, 0.003100, 0.002461, 0.001953, 0.001550,
    0.001230, 0.000977, 0.000775, 0.000615, 0.000488, 0.000388, 0.000308, 0.000244,
    0.000194, 0.000154, 0.000122, 0.000097, 0.000077, 0.000061, 0.000048, 0.000038,
    0.000031, 0.000024, 0.000019, 0.000015, 0.000012, 0.000010, 0.000008, 0.000006,
    0.000005, 0.000004, 0.000003, 0.000002, 0.000002, 0.000002, 0.000001, 1e-20 );

  GWindowTbl: array[0..511] of Double = (
    -0.000000,-0.000443, 0.003250,-0.007004, 0.031082,-0.078629, 0.100311,-0.572037, 1.144989, 0.572037, 0.100311, 0.078629, 0.031082, 0.007004, 0.003250, 0.000443,
    -0.000015,-0.000473, 0.003326,-0.007919, 0.030518,-0.084183, 0.090927,-0.600220, 1.144287, 0.543823, 0.108856, 0.073059, 0.031479, 0.006119, 0.003174, 0.000397,
    -0.000015,-0.000534, 0.003387,-0.008865, 0.029785,-0.089706, 0.080688,-0.628296, 1.142212, 0.515610, 0.116577, 0.067520, 0.031738, 0.005295, 0.003082, 0.000366,
    -0.000015,-0.000580, 0.003433,-0.009842, 0.028885,-0.095169, 0.069595,-0.656219, 1.138763, 0.487473, 0.123474, 0.061996, 0.031845, 0.004486, 0.002991, 0.000320,
    -0.000015,-0.000626, 0.003464,-0.010849, 0.027802,-0.100540, 0.057617,-0.683914, 1.133926, 0.459473, 0.129578, 0.056534, 0.031815, 0.003723, 0.002899, 0.000290,
    -0.000015,-0.000687, 0.003479,-0.011887, 0.026535,-0.105820, 0.044785,-0.711319, 1.127747, 0.431656, 0.134888, 0.051132, 0.031662, 0.003006, 0.002792, 0.000259,
    -0.000015,-0.000748, 0.003479,-0.012939, 0.025085,-0.110947, 0.031082,-0.738373, 1.120224, 0.404083, 0.139450, 0.045837, 0.031387, 0.002335, 0.002686, 0.000244,
    -0.000031,-0.000809, 0.003464,-0.014023, 0.023422,-0.115921, 0.016510,-0.765030, 1.111374, 0.376801, 0.143265, 0.040634, 0.031006, 0.001694, 0.002579, 0.000214,
    -0.000031,-0.000885, 0.003418,-0.015121, 0.021576,-0.120697, 0.001068,-0.791214, 1.101212, 0.349869, 0.146362, 0.035553, 0.030533, 0.001099, 0.002457, 0.000198,
    -0.000031,-0.000961, 0.003372,-0.016235, 0.019531,-0.125259,-0.015228,-0.816864, 1.089783, 0.323318, 0.148773, 0.030609, 0.029938, 0.000549, 0.002350, 0.000168,
    -0.000031,-0.001038, 0.003281,-0.017349, 0.017258,-0.129562,-0.032379,-0.841949, 1.077118, 0.297211, 0.150497, 0.025818, 0.029282, 0.000031, 0.002243, 0.000153,
    -0.000046,-0.001114, 0.003174,-0.018463, 0.014801,-0.133591,-0.050354,-0.866364, 1.063217, 0.271591, 0.151596, 0.021179, 0.028534,-0.000443, 0.002121, 0.000137,
    -0.000046,-0.001205, 0.003052,-0.019577, 0.012115,-0.137299,-0.069168,-0.890091, 1.048157, 0.246506, 0.152069, 0.016708, 0.027725,-0.000870, 0.002014, 0.000122,
    -0.000061,-0.001297, 0.002884,-0.020691, 0.009232,-0.140671,-0.088776,-0.913055, 1.031937, 0.221985, 0.151962, 0.012421, 0.026840,-0.001266, 0.001907, 0.000107,
    -0.000061,-0.001389, 0.002701,-0.021790, 0.006134,-0.143677,-0.109161,-0.935196, 1.014618, 0.198059, 0.151306, 0.008316, 0.025909,-0.001617, 0.001785, 0.000107,
    -0.000076,-0.001480, 0.002487,-0.022858, 0.002823,-0.146255,-0.130310,-0.956482, 0.996246, 0.174789, 0.150116, 0.004395, 0.024933,-0.001938, 0.001694, 0.000092,
    -0.000076,-0.001587, 0.002228,-0.023911,-0.000687,-0.148422,-0.152206,-0.976852, 0.976852, 0.152206, 0.148422, 0.000687, 0.023911,-0.002228, 0.001587, 0.000076,
    -0.000092,-0.001694, 0.001938,-0.024933,-0.004395,-0.150116,-0.174789,-0.996246, 0.956482, 0.130310, 0.146255,-0.002823, 0.022858,-0.002487, 0.001480, 0.000076,
    -0.000107,-0.001785, 0.001617,-0.025909,-0.008316,-0.151306,-0.198059,-1.014618, 0.935196, 0.109161, 0.143677,-0.006134, 0.021790,-0.002701, 0.001389, 0.000061,
    -0.000107,-0.001907, 0.001266,-0.026840,-0.012421,-0.151962,-0.221985,-1.031937, 0.913055, 0.088776, 0.140671,-0.009232, 0.020691,-0.002884, 0.001297, 0.000061,
    -0.000122,-0.002014, 0.000870,-0.027725,-0.016708,-0.152069,-0.246506,-1.048157, 0.890091, 0.069168, 0.137299,-0.012115, 0.019577,-0.003052, 0.001205, 0.000046,
    -0.000137,-0.002121, 0.000443,-0.028534,-0.021179,-0.151596,-0.271591,-1.063217, 0.866364, 0.050354, 0.133591,-0.014801, 0.018463,-0.003174, 0.001114, 0.000046,
    -0.000153,-0.002243,-0.000031,-0.029282,-0.025818,-0.150497,-0.297211,-1.077118, 0.841949, 0.032379, 0.129562,-0.017258, 0.017349,-0.003281, 0.001038, 0.000031,
    -0.000168,-0.002350,-0.000549,-0.029938,-0.030609,-0.148773,-0.323318,-1.089783, 0.816864, 0.015228, 0.125259,-0.019531, 0.016235,-0.003372, 0.000961, 0.000031,
    -0.000198,-0.002457,-0.001099,-0.030533,-0.035553,-0.146362,-0.349869,-1.101212, 0.791214,-0.001068, 0.120697,-0.021576, 0.015121,-0.003418, 0.000885, 0.000031,
    -0.000214,-0.002579,-0.001694,-0.031006,-0.040634,-0.143265,-0.376801,-1.111374, 0.765030,-0.016510, 0.115921,-0.023422, 0.014023,-0.003464, 0.000809, 0.000031,
    -0.000244,-0.002686,-0.002335,-0.031387,-0.045837,-0.139450,-0.404083,-1.120224, 0.738373,-0.031082, 0.110947,-0.025085, 0.012939,-0.003479, 0.000748, 0.000015,
    -0.000259,-0.002792,-0.003006,-0.031662,-0.051132,-0.134888,-0.431656,-1.127747, 0.711319,-0.044785, 0.105820,-0.026535, 0.011887,-0.003479, 0.000687, 0.000015,
    -0.000290,-0.002899,-0.003723,-0.031815,-0.056534,-0.129578,-0.459473,-1.133926, 0.683914,-0.057617, 0.100540,-0.027802, 0.010849,-0.003464, 0.000626, 0.000015,
    -0.000320,-0.002991,-0.004486,-0.031845,-0.061996,-0.123474,-0.487473,-1.138763, 0.656219,-0.069595, 0.095169,-0.028885, 0.009842,-0.003433, 0.000580, 0.000015,
    -0.000366,-0.003082,-0.005295,-0.031738,-0.067520,-0.116577,-0.515610,-1.142212, 0.628296,-0.080688, 0.089706,-0.029785, 0.008865,-0.003387, 0.000534, 0.000015,
    -0.000397,-0.003174,-0.006119,-0.031479,-0.073059,-0.108856,-0.543823,-1.144287, 0.600220,-0.090927, 0.084183,-0.030518, 0.007919,-0.003326, 0.000473, 0.000015 );

const // 2d arrays
  GFilterTbl: array[0..63, 0..31] of Double = (
    (0.707107, -0.707107, -0.707107, 0.707107, 0.707107, -0.707107, -0.707107, 0.707107, 0.707107, -0.707107, -0.707107, 0.707107, 0.707107, -0.707107, -0.707107, 0.707107, 0.707107, -0.707107, -0.707107,
    0.707107, 0.707107, -0.707107, -0.707107, 0.707107, 0.707107, -0.707107, -0.707107, 0.707107, 0.707107, -0.707107, -0.707107, 0.707107),
    (0.671559, -0.803208, -0.514103, 0.903989, 0.336890, -0.970031, -0.146730, 0.998795, -0.049068, -0.989177, 0.242980, 0.941544, -0.427555, -0.857729, 0.595699, 0.740951, -0.740951, -0.595699, 0.857729,
    0.427555, -0.941544, -0.242980, 0.989177, 0.049068, -0.998795, 0.146730, 0.970031, -0.336890, -0.903989, 0.514103, 0.803208, -0.671559),
    (0.634393, -0.881921, -0.290285, 0.995185, -0.098017, -0.956940, 0.471397, 0.773010, -0.773010, -0.471397, 0.956940, 0.098017, -0.995185, 0.290285, 0.881921, -0.634393, -0.634393, 0.881921, 0.290285,
    -0.995185, 0.098017, 0.956940, -0.471397, -0.773010, 0.773010, 0.471397, -0.956940, -0.098017, 0.995185, -0.290285, -0.881921, 0.634393),
    (0.595699, -0.941544, -0.049068, 0.970031, -0.514103, -0.671559, 0.903989, 0.146730, -0.989177, 0.427555, 0.740951, -0.857729, -0.242980, 0.998795, -0.336890, -0.803208, 0.803208, 0.336890, -0.998795,
    0.242980, 0.857729, -0.740951, -0.427555, 0.989177, -0.146730, -0.903989, 0.671559, 0.514103, -0.970031, 0.049068, 0.941544, -0.595699),
    (0.555570, -0.980785, 0.195090, 0.831470, -0.831470, -0.195090, 0.980785, -0.555570, -0.555570, 0.980785, -0.195090, -0.831470, 0.831470, 0.195090, -0.980785, 0.555570, 0.555570, -0.980785, 0.195090,
    0.831470, -0.831470, -0.195090, 0.980785, -0.555570, -0.555570, 0.980785, -0.195090, -0.831470, 0.831470, 0.195090, -0.980785, 0.555570),
    (0.514103, -0.998795, 0.427555, 0.595699, -0.989177, 0.336890, 0.671559, -0.970031, 0.242980, 0.740951, -0.941544, 0.146730, 0.803208, -0.903989, 0.049068, 0.857729, -0.857729, -0.049068, 0.903989,
    -0.803208, -0.146730, 0.941544, -0.740951, -0.242980, 0.970031, -0.671559, -0.336890, 0.989177, -0.595699, -0.427555, 0.998795, -0.514103),
    (0.471397, -0.995185, 0.634393, 0.290285, -0.956940, 0.773010, 0.098017, -0.881921, 0.881921, -0.098017, -0.773010, 0.956940, -0.290285, -0.634393, 0.995185, -0.471397, -0.471397, 0.995185, -0.634393,
    -0.290285, 0.956940, -0.773010, -0.098017, 0.881921, -0.881921, 0.098017, 0.773010, -0.956940, 0.290285, 0.634393, -0.995185, 0.471397),
    (0.427555, -0.970031, 0.803208, -0.049068, -0.740951, 0.989177, -0.514103, -0.336890, 0.941544, -0.857729, 0.146730, 0.671559, -0.998795, 0.595699, 0.242980, -0.903989, 0.903989, -0.242980, -0.595699,
    0.998795, -0.671559, -0.146730, 0.857729, -0.941544, 0.336890, 0.514103, -0.989177, 0.740951, 0.049068, -0.803208, 0.970031, -0.427555),
    (0.382683, -0.923880, 0.923880, -0.382683, -0.382683, 0.923880, -0.923880, 0.382683, 0.382683, -0.923880, 0.923880, -0.382683, -0.382683, 0.923880, -0.923880, 0.382683, 0.382683, -0.923880, 0.923880,
    -0.382683, -0.382683, 0.923880, -0.923880, 0.382683, 0.382683, -0.923880, 0.923880, -0.382683, -0.382683, 0.923880, -0.923880, 0.382683),
    (0.336890, -0.857729, 0.989177, -0.671559, 0.049068, 0.595699, -0.970031, 0.903989, -0.427555, -0.242980, 0.803208, -0.998795, 0.740951, -0.146730, -0.514103, 0.941544, -0.941544, 0.514103, 0.146730,
    -0.740951, 0.998795, -0.803208, 0.242980, 0.427555, -0.903989, 0.970031, -0.595699, -0.049068, 0.671559, -0.989177, 0.857729, -0.336890),
    (0.290285, -0.773010, 0.995185, -0.881921, 0.471397, 0.098017, -0.634393, 0.956940, -0.956940, 0.634393, -0.098017, -0.471397, 0.881921, -0.995185, 0.773010, -0.290285, -0.290285, 0.773010, -0.995185,
    0.881921, -0.471397, -0.098017, 0.634393, -0.956940, 0.956940, -0.634393, 0.098017, 0.471397, -0.881921, 0.995185, -0.773010, 0.290285),
    (0.242980, -0.671559, 0.941544, -0.989177, 0.803208, -0.427555, -0.049068, 0.514103, -0.857729, 0.998795, -0.903989, 0.595699, -0.146730, -0.336890, 0.740951, -0.970031, 0.970031, -0.740951, 0.336890,
    0.146730, -0.595699, 0.903989, -0.998795, 0.857729, -0.514103, 0.049068, 0.427555, -0.803208, 0.989177, -0.941544, 0.671559, -0.242980),
    (0.195090, -0.555570, 0.831470, -0.980785, 0.980785, -0.831470, 0.555570, -0.195090, -0.195090, 0.555570, -0.831470, 0.980785, -0.980785, 0.831470, -0.555570, 0.195090, 0.195090, -0.555570, 0.831470,
    -0.980785, 0.980785, -0.831470, 0.555570, -0.195090, -0.195090, 0.555570, -0.831470, 0.980785, -0.980785, 0.831470, -0.555570, 0.195090),
    (0.146730, -0.427555, 0.671559, -0.857729, 0.970031, -0.998795, 0.941544, -0.803208, 0.595699, -0.336890, 0.049068, 0.242980, -0.514103, 0.740951, -0.903989, 0.989177, -0.989177, 0.903989, -0.740951,
    0.514103, -0.242980, -0.049068, 0.336890, -0.595699, 0.803208, -0.941544, 0.998795, -0.970031, 0.857729, -0.671559, 0.427555, -0.146730),
    (0.098017, -0.290285, 0.471397, -0.634393, 0.773010, -0.881921, 0.956940, -0.995185, 0.995185, -0.956940, 0.881921, -0.773010, 0.634393, -0.471397, 0.290285, -0.098017, -0.098017, 0.290285, -0.471397,
    0.634393, -0.773010, 0.881921, -0.956940, 0.995185, -0.995185, 0.956940, -0.881921, 0.773010, -0.634393, 0.471397, -0.290285, 0.098017),
    (0.049068, -0.146730, 0.242980, -0.336890, 0.427555, -0.514103, 0.595699, -0.671559, 0.740951, -0.803208, 0.857729, -0.903989, 0.941544, -0.970031, 0.989177, -0.998795, 0.998795, -0.989177, 0.970031,
    -0.941544, 0.903989, -0.857729, 0.803208, -0.740951, 0.671559, -0.595699, 0.514103, -0.427555, 0.336890, -0.242980, 0.146730, -0.049068),
    (0.000000, -0.000000, 0.000000, -0.000000, 0.000000, -0.000000, -0.000000, -0.000000, -0.000000, -0.000000, -0.000000, -0.000000, -0.000000, -0.000000, -0.000000, -0.000000, 0.000000, -0.000000,
    0.000000, -0.000000, 0.000000, -0.000000, 0.000000, 0.000000, 0.000000, -0.000000, 0.000000, 0.000000, 0.000000, -0.000000, 0.000000, 0.000000),
    (-0.049068, 0.146730, -0.242980, 0.336890, -0.427555, 0.514103, -0.595699, 0.671559, -0.740951, 0.803208, -0.857729, 0.903989, -0.941544, 0.970031, -0.989177, 0.998795, -0.998795, 0.989177, -0.970031,
    0.941544, -0.903989, 0.857729, -0.803208, 0.740951, -0.671559, 0.595699, -0.514103, 0.427555, -0.336890, 0.242980, -0.146730, 0.049068),
    (-0.098017, 0.290285, -0.471397, 0.634393, -0.773010, 0.881921, -0.956940, 0.995185, -0.995185, 0.956940, -0.881921, 0.773010, -0.634393, 0.471397, -0.290285, 0.098017, 0.098017, -0.290285, 0.471397,
    -0.634393, 0.773010, -0.881921, 0.956940, -0.995185, 0.995185, -0.956940, 0.881921, -0.773010, 0.634393, -0.471397, 0.290285, -0.098017),
    (-0.146730, 0.427555, -0.671559, 0.857729, -0.970031, 0.998795, -0.941544, 0.803208, -0.595699, 0.336890, -0.049068, -0.242980, 0.514103, -0.740951, 0.903989, -0.989177, 0.989177, -0.903989, 0.740951,
    -0.514103, 0.242980, 0.049068, -0.336890, 0.595699, -0.803208, 0.941544, -0.998795, 0.970031, -0.857729, 0.671559, -0.427555, 0.146730),
    (-0.195090, 0.555570, -0.831470, 0.980785, -0.980785, 0.831470, -0.555570, 0.195090, 0.195090, -0.555570, 0.831470, -0.980785, 0.980785, -0.831470, 0.555570, -0.195090, -0.195090, 0.555570, -0.831470,
    0.980785, -0.980785, 0.831470, -0.555570, 0.195090, 0.195090, -0.555570, 0.831470, -0.980785, 0.980785, -0.831470, 0.555570, -0.195090),
    (-0.242980, 0.671559, -0.941544, 0.989177, -0.803208, 0.427555, 0.049068, -0.514103, 0.857729, -0.998795, 0.903989, -0.595699, 0.146730, 0.336890, -0.740951, 0.970031, -0.970031, 0.740951, -0.336890,
    -0.146730, 0.595699, -0.903989, 0.998795, -0.857729, 0.514103, -0.049068, -0.427555, 0.803208, -0.989177, 0.941544, -0.671559, 0.242980),
    (-0.290285, 0.773010, -0.995185, 0.881921, -0.471397, -0.098017, 0.634393, -0.956940, 0.956940, -0.634393, 0.098017, 0.471397, -0.881921, 0.995185, -0.773010, 0.290285, 0.290285, -0.773010, 0.995185,
    -0.881921, 0.471397, 0.098017, -0.634393, 0.956940, -0.956940, 0.634393, -0.098017, -0.471397, 0.881921, -0.995185, 0.773010, -0.290285),
    (-0.336890, 0.857729, -0.989177, 0.671559, -0.049068, -0.595699, 0.970031, -0.903989, 0.427555, 0.242980, -0.803208, 0.998795, -0.740951, 0.146730, 0.514103, -0.941544, 0.941544, -0.514103, -0.146730,
    0.740951, -0.998795, 0.803208, -0.242980, -0.427555, 0.903989, -0.970031, 0.595699, 0.049068, -0.671559, 0.989177, -0.857729, 0.336890),
    (-0.382683, 0.923880, -0.923880, 0.382683, 0.382683, -0.923880, 0.923880, -0.382683, -0.382683, 0.923880, -0.923880, 0.382683, 0.382683, -0.923880, 0.923880, -0.382683, -0.382683, 0.923880, -0.923880,
    0.382683, 0.382683, -0.923880, 0.923880, -0.382683, -0.382683, 0.923880, -0.923880, 0.382683, 0.382683, -0.923880, 0.923880, -0.382683),
    (-0.427555, 0.970031, -0.803208, 0.049068, 0.740951, -0.989177, 0.514103, 0.336890, -0.941544, 0.857729, -0.146730, -0.671559, 0.998795, -0.595699, -0.242980, 0.903989, -0.903989, 0.242980, 0.595699,
    -0.998795, 0.671559, 0.146730, -0.857729, 0.941544, -0.336890, -0.514103, 0.989177, -0.740951, -0.049068, 0.803208, -0.970031, 0.427555),
    (-0.471397, 0.995185, -0.634393, -0.290285, 0.956940, -0.773010, -0.098017, 0.881921, -0.881921, 0.098017, 0.773010, -0.956940, 0.290285, 0.634393, -0.995185, 0.471397, 0.471397, -0.995185, 0.634393,
    0.290285, -0.956940, 0.773010, 0.098017, -0.881921, 0.881921, -0.098017, -0.773010, 0.956940, -0.290285, -0.634393, 0.995185, -0.471397),
    (-0.514103, 0.998795, -0.427555, -0.595699, 0.989177, -0.336890, -0.671559, 0.970031, -0.242980, -0.740951, 0.941544, -0.146730, -0.803208, 0.903989, -0.049068, -0.857729, 0.857729, 0.049068,
    -0.903989, 0.803208, 0.146730, -0.941544, 0.740951, 0.242980, -0.970031, 0.671559, 0.336890, -0.989177, 0.595699, 0.427555, -0.998795, 0.514103),
    (-0.555570, 0.980785, -0.195090, -0.831470, 0.831470, 0.195090, -0.980785, 0.555570, 0.555570, -0.980785, 0.195090, 0.831470, -0.831470, -0.195090, 0.980785, -0.555570, -0.555570, 0.980785, -0.195090,
    -0.831470, 0.831470, 0.195090, -0.980785, 0.555570, 0.555570, -0.980785, 0.195090, 0.831470, -0.831470, -0.195090, 0.980785, -0.555570),
    (-0.595699, 0.941544, 0.049068, -0.970031, 0.514103, 0.671559, -0.903989, -0.146730, 0.989177, -0.427555, -0.740951, 0.857729, 0.242980, -0.998795, 0.336890, 0.803208, -0.803208, -0.336890, 0.998795,
    -0.242980, -0.857729, 0.740951, 0.427555, -0.989177, 0.146730, 0.903989, -0.671559, -0.514103, 0.970031, -0.049068, -0.941544, 0.595699),
    (-0.634393, 0.881921, 0.290285, -0.995185, 0.098017, 0.956940, -0.471397, -0.773010, 0.773010, 0.471397, -0.956940, -0.098017, 0.995185, -0.290285, -0.881921, 0.634393, 0.634393, -0.881921, -0.290285,
    0.995185, -0.098017, -0.956940, 0.471397, 0.773010, -0.773010, -0.471397, 0.956940, 0.098017, -0.995185, 0.290285, 0.881921, -0.634393),
    (-0.671559, 0.803208, 0.514103, -0.903989, -0.336890, 0.970031, 0.146730, -0.998795, 0.049068, 0.989177, -0.242980, -0.941544, 0.427555, 0.857729, -0.595699, -0.740951, 0.740951, 0.595699, -0.857729,
    -0.427555, 0.941544, 0.242980, -0.989177, -0.049068, 0.998795, -0.146730, -0.970031, 0.336890, 0.903989, -0.514103, -0.803208, 0.671559),
    (-0.707107, 0.707107, 0.707107, -0.707107, -0.707107, 0.707107, 0.707107, -0.707107, -0.707107, 0.707107, 0.707107, -0.707107, -0.707107, 0.707107, 0.707107, -0.707107, -0.707107, 0.707107, 0.707107,
    -0.707107, -0.707107, 0.707107, 0.707107, -0.707107, -0.707107, 0.707107, 0.707107, -0.707107, -0.707107, 0.707107, 0.707107, -0.707107),
    (-0.740951, 0.595699, 0.857729, -0.427555, -0.941544, 0.242980, 0.989177, -0.049068, -0.998795, -0.146730, 0.970031, 0.336890, -0.903989, -0.514103, 0.803208, 0.671559, -0.671559, -0.803208, 0.514103,
    0.903989, -0.336890, -0.970031, 0.146730, 0.998795, 0.049068, -0.989177, -0.242980, 0.941544, 0.427555, -0.857729, -0.595699, 0.740951),
    (-0.773010, 0.471397, 0.956940, -0.098017, -0.995185, -0.290285, 0.881921, 0.634393, -0.634393, -0.881921, 0.290285, 0.995185, 0.098017, -0.956940, -0.471397, 0.773010, 0.773010, -0.471397, -0.956940,
    0.098017, 0.995185, 0.290285, -0.881921, -0.634393, 0.634393, 0.881921, -0.290285, -0.995185, -0.098017, 0.956940, 0.471397, -0.773010),
    (-0.803208, 0.336890, 0.998795, 0.242980, -0.857729, -0.740951, 0.427555, 0.989177, 0.146730, -0.903989, -0.671559, 0.514103, 0.970031, 0.049068, -0.941544, -0.595699, 0.595699, 0.941544, -0.049068,
    -0.970031, -0.514103, 0.671559, 0.903989, -0.146730, -0.989177, -0.427555, 0.740951, 0.857729, -0.242980, -0.998795, -0.336890, 0.803208),
    (-0.831470, 0.195090, 0.980785, 0.555570, -0.555570, -0.980785, -0.195090, 0.831470, 0.831470, -0.195090, -0.980785, -0.555570, 0.555570, 0.980785, 0.195090, -0.831470, -0.831470, 0.195090, 0.980785,
    0.555570, -0.555570, -0.980785, -0.195090, 0.831470, 0.831470, -0.195090, -0.980785, -0.555570, 0.555570, 0.980785, 0.195090, -0.831470),
    (-0.857729, 0.049068, 0.903989, 0.803208, -0.146730, -0.941544, -0.740951, 0.242980, 0.970031, 0.671559, -0.336890, -0.989177, -0.595699, 0.427555, 0.998795, 0.514103, -0.514103, -0.998795, -0.427555,
    0.595699, 0.989177, 0.336890, -0.671559, -0.970031, -0.242980, 0.740951, 0.941544, 0.146730, -0.803208, -0.903989, -0.049068, 0.857729),
    (-0.881921, -0.098017, 0.773010, 0.956940, 0.290285, -0.634393, -0.995185, -0.471397, 0.471397, 0.995185, 0.634393, -0.290285, -0.956940, -0.773010, 0.098017, 0.881921, 0.881921, 0.098017, -0.773010,
    -0.956940, -0.290285, 0.634393, 0.995185, 0.471397, -0.471397, -0.995185, -0.634393, 0.290285, 0.956940, 0.773010, -0.098017, -0.881921),
    (-0.903989, -0.242980, 0.595699, 0.998795, 0.671559, -0.146730, -0.857729, -0.941544, -0.336890, 0.514103, 0.989177, 0.740951, -0.049068, -0.803208, -0.970031, -0.427555, 0.427555, 0.970031, 0.803208,
    0.049068, -0.740951, -0.989177, -0.514103, 0.336890, 0.941544, 0.857729, 0.146730, -0.671559, -0.998795, -0.595699, 0.242980, 0.903989),
    (-0.923880, -0.382683, 0.382683, 0.923880, 0.923880, 0.382683, -0.382683, -0.923880, -0.923880, -0.382683, 0.382683, 0.923880, 0.923880, 0.382683, -0.382683, -0.923880, -0.923880, -0.382683, 0.382683,
    0.923880, 0.923880, 0.382683, -0.382683, -0.923880, -0.923880, -0.382683, 0.382683, 0.923880, 0.923880, 0.382683, -0.382683, -0.923880),
    (-0.941544, -0.514103, 0.146730, 0.740951, 0.998795, 0.803208, 0.242980, -0.427555, -0.903989, -0.970031, -0.595699, 0.049068, 0.671559, 0.989177, 0.857729, 0.336890, -0.336890, -0.857729, -0.989177,
    -0.671559, -0.049068, 0.595699, 0.970031, 0.903989, 0.427555, -0.242980, -0.803208, -0.998795, -0.740951, -0.146730, 0.514103, 0.941544),
    (-0.956940, -0.634393, -0.098017, 0.471397, 0.881921, 0.995185, 0.773010, 0.290285, -0.290285, -0.773010, -0.995185, -0.881921, -0.471397, 0.098017, 0.634393, 0.956940, 0.956940, 0.634393, 0.098017,
    -0.471397, -0.881921, -0.995185, -0.773010, -0.290285, 0.290285, 0.773010, 0.995185, 0.881921, 0.471397, -0.098017, -0.634393, -0.956940),
    (-0.970031, -0.740951, -0.336890, 0.146730, 0.595699, 0.903989, 0.998795, 0.857729, 0.514103, 0.049068, -0.427555, -0.803208, -0.989177, -0.941544, -0.671559, -0.242980, 0.242980, 0.671559, 0.941544,
    0.989177, 0.803208, 0.427555, -0.049068, -0.514103, -0.857729, -0.998795, -0.903989, -0.595699, -0.146730, 0.336890, 0.740951, 0.970031),
    (-0.980785, -0.831470, -0.555570, -0.195090, 0.195090, 0.555570, 0.831470, 0.980785, 0.980785, 0.831470, 0.555570, 0.195090, -0.195090, -0.555570, -0.831470, -0.980785, -0.980785, -0.831470, -0.555570,
    -0.195090, 0.195090, 0.555570, 0.831470, 0.980785, 0.980785, 0.831470, 0.555570, 0.195090, -0.195090, -0.555570, -0.831470, -0.980785),
    (-0.989177, -0.903989, -0.740951, -0.514103, -0.242980, 0.049068, 0.336890, 0.595699, 0.803208, 0.941544, 0.998795, 0.970031, 0.857729, 0.671559, 0.427555, 0.146730, -0.146730, -0.427555, -0.671559,
    -0.857729, -0.970031, -0.998795, -0.941544, -0.803208, -0.595699, -0.336890, -0.049068, 0.242980, 0.514103, 0.740951, 0.903989, 0.989177),
    (-0.995185, -0.956940, -0.881921, -0.773010, -0.634393, -0.471397, -0.290285, -0.098017, 0.098017, 0.290285, 0.471397, 0.634393, 0.773010, 0.881921, 0.956940, 0.995185, 0.995185, 0.956940, 0.881921,
    0.773010, 0.634393, 0.471397, 0.290285, 0.098017, -0.098017, -0.290285, -0.471397, -0.634393, -0.773010, -0.881921, -0.956940, -0.995185),
    (-0.998795, -0.989177, -0.970031, -0.941544, -0.903989, -0.857729, -0.803208, -0.740951, -0.671559, -0.595699, -0.514103, -0.427555, -0.336890, -0.242980, -0.146730, -0.049068, 0.049068, 0.146730,
    0.242980, 0.336890, 0.427555, 0.514103, 0.595699, 0.671559, 0.740951, 0.803208, 0.857729, 0.903989, 0.941544, 0.970031, 0.989177, 0.998795),
    (-1.000000, -1.000000, -1.000000, -1.000000, -1.000000, -1.000000, -1.000000, -1.000000, -1.000000, -1.000000, -1.000000, -1.000000, -1.000000, -1.000000, -1.000000, -1.000000, -1.000000, -1.000000,
    -1.000000, -1.000000, -1.000000, -1.000000, -1.000000, -1.000000, -1.000000, -1.000000, -1.000000, -1.000000, -1.000000, -1.000000, -1.000000, -1.000000),
    (-0.998795, -0.989177, -0.970031, -0.941544, -0.903989, -0.857729, -0.803208, -0.740951, -0.671559, -0.595699, -0.514103, -0.427555, -0.336890, -0.242980, -0.146730, -0.049068, 0.049068, 0.146730,
    0.242980, 0.336890, 0.427555, 0.514103, 0.595699, 0.671559, 0.740951, 0.803208, 0.857729, 0.903989, 0.941544, 0.970031, 0.989177, 0.998795),
    (-0.995185, -0.956940, -0.881921, -0.773010, -0.634393, -0.471397, -0.290285, -0.098017, 0.098017, 0.290285, 0.471397, 0.634393, 0.773010, 0.881921, 0.956940, 0.995185, 0.995185, 0.956940, 0.881921,
    0.773010, 0.634393, 0.471397, 0.290285, 0.098017, -0.098017, -0.290285, -0.471397, -0.634393, -0.773010, -0.881921, -0.956940, -0.995185),
    (-0.989177, -0.903989, -0.740951, -0.514103, -0.242980, 0.049068, 0.336890, 0.595699, 0.803208, 0.941544, 0.998795, 0.970031, 0.857729, 0.671559, 0.427555, 0.146730, -0.146730, -0.427555, -0.671559,
    -0.857729, -0.970031, -0.998795, -0.941544, -0.803208, -0.595699, -0.336890, -0.049068, 0.242980, 0.514103, 0.740951, 0.903989, 0.989177),
    (-0.980785, -0.831470, -0.555570, -0.195090, 0.195090, 0.555570, 0.831470, 0.980785, 0.980785, 0.831470, 0.555570, 0.195090, -0.195090, -0.555570, -0.831470, -0.980785, -0.980785, -0.831470, -0.555570,
    -0.195090, 0.195090, 0.555570, 0.831470, 0.980785, 0.980785, 0.831470, 0.555570, 0.195090, -0.195090, -0.555570, -0.831470, -0.980785),
    (-0.970031, -0.740951, -0.336890, 0.146730, 0.595699, 0.903989, 0.998795, 0.857729, 0.514103, 0.049068, -0.427555, -0.803208, -0.989177, -0.941544, -0.671559, -0.242980, 0.242980, 0.671559, 0.941544,
    0.989177, 0.803208, 0.427555, -0.049068, -0.514103, -0.857729, -0.998795, -0.903989, -0.595699, -0.146730, 0.336890, 0.740951, 0.970031),
    (-0.956940, -0.634393, -0.098017, 0.471397, 0.881921, 0.995185, 0.773010, 0.290285, -0.290285, -0.773010, -0.995185, -0.881921, -0.471397, 0.098017, 0.634393, 0.956940, 0.956940, 0.634393, 0.098017,
    -0.471397, -0.881921, -0.995185, -0.773010, -0.290285, 0.290285, 0.773010, 0.995185, 0.881921, 0.471397, -0.098017, -0.634393, -0.956940),
    (-0.941544, -0.514103, 0.146730, 0.740951, 0.998795, 0.803208, 0.242980, -0.427555, -0.903989, -0.970031, -0.595699, 0.049068, 0.671559, 0.989177, 0.857729, 0.336890, -0.336890, -0.857729, -0.989177,
    -0.671559, -0.049068, 0.595699, 0.970031, 0.903989, 0.427555, -0.242980, -0.803208, -0.998795, -0.740951, -0.146730, 0.514103, 0.941544),
    (-0.923880, -0.382683, 0.382683, 0.923880, 0.923880, 0.382683, -0.382683, -0.923880, -0.923880, -0.382683, 0.382683, 0.923880, 0.923880, 0.382683, -0.382683, -0.923880, -0.923880, -0.382683, 0.382683,
    0.923880, 0.923880, 0.382683, -0.382683, -0.923880, -0.923880, -0.382683, 0.382683, 0.923880, 0.923880, 0.382683, -0.382683, -0.923880),
    (-0.903989, -0.242980, 0.595699, 0.998795, 0.671559, -0.146730, -0.857729, -0.941544, -0.336890, 0.514103, 0.989177, 0.740951, -0.049068, -0.803208, -0.970031, -0.427555, 0.427555, 0.970031, 0.803208,
    0.049068, -0.740951, -0.989177, -0.514103, 0.336890, 0.941544, 0.857729, 0.146730, -0.671559, -0.998795, -0.595699, 0.242980, 0.903989),
    (-0.881921, -0.098017, 0.773010, 0.956940, 0.290285, -0.634393, -0.995185, -0.471397, 0.471397, 0.995185, 0.634393, -0.290285, -0.956940, -0.773010, 0.098017, 0.881921, 0.881921, 0.098017, -0.773010,
    -0.956940, -0.290285, 0.634393, 0.995185, 0.471397, -0.471397, -0.995185, -0.634393, 0.290285, 0.956940, 0.773010, -0.098017, -0.881921),
    (-0.857729, 0.049068, 0.903989, 0.803208, -0.146730, -0.941544, -0.740951, 0.242980, 0.970031, 0.671559, -0.336890, -0.989177, -0.595699, 0.427555, 0.998795, 0.514103, -0.514103, -0.998795, -0.427555,
    0.595699, 0.989177, 0.336890, -0.671559, -0.970031, -0.242980, 0.740951, 0.941544, 0.146730, -0.803208, -0.903989, -0.049068, 0.857729),
    (-0.831470, 0.195090, 0.980785, 0.555570, -0.555570, -0.980785, -0.195090, 0.831470, 0.831470, -0.195090, -0.980785, -0.555570, 0.555570, 0.980785, 0.195090, -0.831470, -0.831470, 0.195090, 0.980785,
    0.555570, -0.555570, -0.980785, -0.195090, 0.831470, 0.831470, -0.195090, -0.980785, -0.555570, 0.555570, 0.980785, 0.195090, -0.831470),
    (-0.803208, 0.336890, 0.998795, 0.242980, -0.857729, -0.740951, 0.427555, 0.989177, 0.146730, -0.903989, -0.671559, 0.514103, 0.970031, 0.049068, -0.941544, -0.595699, 0.595699, 0.941544, -0.049068,
    -0.970031, -0.514103, 0.671559, 0.903989, -0.146730, -0.989177, -0.427555, 0.740951, 0.857729, -0.242980, -0.998795, -0.336890, 0.803208),
    (-0.773010, 0.471397, 0.956940, -0.098017, -0.995185, -0.290285, 0.881921, 0.634393, -0.634393, -0.881921, 0.290285, 0.995185, 0.098017, -0.956940, -0.471397, 0.773010, 0.773010, -0.471397, -0.956940,
    0.098017, 0.995185, 0.290285, -0.881921, -0.634393, 0.634393, 0.881921, -0.290285, -0.995185, -0.098017, 0.956940, 0.471397, -0.773010),
    (-0.740951, 0.595699, 0.857729, -0.427555, -0.941544, 0.242980, 0.989177, -0.049068, -0.998795, -0.146730, 0.970031, 0.336890, -0.903989, -0.514103, 0.803208, 0.671559, -0.671559, -0.803208, 0.514103,
    0.903989, -0.336890, -0.970031, 0.146730, 0.998795, 0.049068, -0.989177, -0.242980, 0.941544, 0.427555, -0.857729, -0.595699, 0.740951));

function jo_readBits(data: PByte; var at: Integer; num: Integer): Cardinal;
var
  iByte, iBit: Integer;
begin
  Result := 0;
  iByte := at div 8; // byte index
  iBit := at mod 8; // bit index (in byte)
  Inc(data, iByte);

  { Form a word of the next four bytes }
  Result := Result or (data^ shl 24);
  Inc(data);
  Result := Result or (data^ shl 16);
  Inc(data);
  Result := Result or (data^ shl 8);
  Inc(data);
  Result := Result or (data^ shl 0);

  { Remove bits already used }
  Result := Result shl iBit;

  { Remove bits after the desired bits }
  Result := Result shr (32 - num);

  Inc(at, num);
end;

procedure jo_mp1_init(out h: TMp1DecoderContext);
var
  i, j: Integer;
begin
  h.kbps := 0;
  h.channels := 0;
  h.hz := 0;
  h.BlockSize := 0;

  // Buffers for the lapped transform
  for i := 0 to 1 do
    for j := 0 to 1023 do
      h.Buf[i][j] := 0.0;

  h.bufOffset[0] := 64;
  h.bufOffset[1] := 64;
end;

function jo_mp1_decode(var h: TMp1DecoderContext;
  const input: Pointer; inputSize: Integer;
  var output: TSamplesArray): Integer;
const
  SHRT_MAX = High(SmallInt); // 32767;
  SHRT_MIN = Low(SmallInt); // (-32767 - 1);
var
  SamplesCount: Integer;
  at, atBlock: Integer;
  //atPrev: Integer;
  i, j, k, n: Integer;
  mode, modeExt: Integer;
  errorProtection: Boolean;
  scaleIdx: array[0..31, 0..1] of Byte;
  pcm: array[0..11, 0..1, 0..31] of SmallInt; // Int16
  ShortSum: Integer;
  sum: Double;
  samples: array[0..31, 0..1] of Integer;
  bitAlloc: array[0..31, 0..1] of Byte;
  bound: Integer;
  samp: Integer;
  f: Double;
  b: Integer;
  bb, bbb: Double;
  bandTbl: array[0..1, 0..31] of Double;
  header: LongWord;
  bufPos: Integer;
  data: PByte;
  ch: Integer;
  iSample: Integer;
  iWin: Integer;
begin
  Result := 0;
  data := input;

  //WriteLn(Format('Total size = $%x', [inputSize]));
  at := 0; // Where in the stream are we? (bit index)

  // Sync markers are byte aligned
  //at := (at+7) and -8; // FFFFFFF8
  at := (at+7) and $FFFFFFF8;
  while (at < inputSize*8-32) do
  begin
    if (data[at div 8] = $FF) and ((data[(at div 8)+1] and $F0) = $F0) then
      Break
    else
      Inc(at, 8);
  end;
  if (at >= inputSize*8-32) then
    Exit(MP1_ERR_SYNC_MARK);

  //WriteLn(Format('Block pos=$%x.%d', [(at div 8), (at mod 8)]));
  atBlock := at;

  header := jo_readBits(data, at, 32);
  //printf("header: %x.%x/%x: %08x\n", (at-32)/8, (at-32)&7, inputSize, header);

  // sync = 0xFFF
  // ID = 2 (MPEG Version 2)
  // layer = 3 (layer 1)
  if ((header and $FFFE0000) <> $FFFE0000) then
  begin
    //WriteLn(Format('Wrong format pos=$%x.%d', [(at div 8), (at mod 8)]));
    Exit(MP1_ERR_HEADER);
  end;

  h.kbps := bitrateTbl2[(header shr 12) and 15];
  if (h.kbps < 0) then Exit;

  h.hz := hzTbl[(header shr 10) and 3];
  if (h.hz = 0) then Exit;

  // mode 0 = stereo
  // mode 1 = joint stereo
  // mode 2 = dual channel (no idea what this does TODO)
  // mode 3 = mono

  mode := (header shr 6) and 3;
  modeExt := (header shr 4) and 3;

  if mode = 3 then
    h.channels := 1
  else
    h.channels := 2;

  if mode = 1 then
    bound := (modeExt+1) * 4
  else
    bound := 32;

  errorProtection := True;
  if (header shr 16) and 1 xor 1 = 0 then
    errorProtection := false;
  if (errorProtection) then
    at := at + 16; // skip the CRC.

  h.BlockSize := Trunc((h.kbps * 1000 / 8) / (h.hz / (384 * h.channels)));

  //WriteLn(Format('  ch=%d br=%d sr=%d mode=%d bound=%d bl_size=%d', [h.channels, h.kbps, h.hz, mode, bound, h.BlockSize]));
  //atPrev := at;

  //FillChar(bitAlloc, SizeOf(bitAlloc), #0);

  // Read bit allocations
  for i := 0 to bound-1 do
  begin
    bitAlloc[i][1] := 0;
    for ch := 0 to h.channels-1 do
      bitAlloc[i][ch] := jo_readBits(data, at, 4);
  end;
  for i := bound to 31 do
  begin
    bitAlloc[i][0] := jo_readBits(data, at, 4);
    bitAlloc[i][1] := bitAlloc[i][0];
  end;
  //WriteLn(Format('BitAlloc size=%d', [(at-atPrev)]));
  //atPrev := at;

  // Read scale indexes
  for i := 0 to 31 do
  begin
    for ch := 0 to h.channels-1 do
    begin
      if bitAlloc[i][ch] <> 0 then
        scaleIdx[i][ch] := jo_readBits(data, at, 6)
      else
        scaleIdx[i][ch] := 63;
    end;
  end;
  //WriteLn(Format('ScaleIdx size=%d', [(at-atPrev)]));
  //atPrev := at;

  // Read & compute output samples
  for iSample := 0 to 11 do
  begin
    // Read normalized, quantized band samples
    for i := 0 to 31 do
      for j := 0 to 1 do
        samples[i][j] := 0;

    for i := 0 to bound-1 do
    begin
      for ch := 0 to h.channels-1 do
      begin
	if (bitAlloc[i][ch] <> 0) then
	  samples[i][ch] := jo_readBits(data, at, bitAlloc[i][ch]+1);
      end;
    end;
    for i := bound to 31 do
    begin
      if (bitAlloc[i][0] <> 0) then
      begin
	samples[i][0] := jo_readBits(data, at, bitAlloc[i][0]+1);
	samples[i][1] := samples[i][0];
      end;
    end;

    // Compute bands: Dequantize & Denormalize
    for i := 0 to 1 do
      for j := 0 to 21 do
	bandTbl[i][j] := 0;

    for i := 0 to 31 do
    begin
      for ch := 0 to h.channels-1 do
      begin
	b := bitAlloc[i][ch];

	if (b <> 0) then
        begin
          Inc(b);
	  samp := samples[i][ch];
	  if ((samp shr (b-1)) and 1) <> 0 then
            f := 0
	  else
            f := -1;

          {
  	double f = ((samp >> b-1) & 1) ? 0 : -1;
  	f += (samp & ((1<<b-1)-1)) / (double)(1<<b-1);
  	f = (f+1.0/(1<<b-1)) * (1<<b)/((1<<b)-1.0);
  	f *= s_jo_multTbl[scaleIdx[i][ch]];
          }

          bb := Double(1 shl (b-1));
          bbb := Double(1 shl b);
	  f := f + ((samp and ((1 shl (b-1))-1)) / bb);
	  f := (f + 1.0/bb) * bbb / (bbb - 1.0);
	  f := f * GMultTbl[scaleIdx[i][ch]];
	  bandTbl[ch][i] := f;
	end;
      end;
    end;

    // Convert subbands to PCM
    for ch := 0 to h.channels-1 do
    begin
      h.bufOffset[ch] := (h.bufOffset[ch] + $3C0) and $3ff;

      bufPos := h.bufOffset[ch];

      for i := 0 to 63 do
      begin
	sum := 0;
	for j := 0 to 31 do
        begin
	  sum := sum + (GFilterTbl[i, j] * bandTbl[ch][j]);
	end;
        h.buf[ch][bufPos+i] := sum;
      end;

      iWin := 0;
      for i := 0 to 31 do
      begin
	sum := 0;
	for j := 0 to 15 do
        begin
	  k := i or ((j + ((j+1) and -2)) shl 5);
	  sum := sum + (GWindowTbl[iWin] * h.buf[ch][(k + h.bufOffset[ch]) and $3ff]);
	  Inc(iWin);
	end;
	ShortSum := Trunc(sum * $8000);

	if ShortSum > SHRT_MAX then
          ShortSum := SHRT_MAX
	else if ShortSum < SHRT_MIN then
          ShortSum := SHRT_MIN;

	pcm[iSample][ch][i] := SmallInt(ShortSum);
      end;
    end;
  end;
  //WriteLn(Format('Samples size=%d', [(at-atPrev)]));
  //WriteLn(Format('Block size=%d (bytes=%d)', [(at-atBlock), (at-atBlock) div 8]));
  if ((at-atBlock) div 8) > (h.BlockSize + 8) then
  begin
  //  WriteLn(Format('Block size too big!', []));
    Exit(MP1_ERR_BLOCK_SIZE);
  end;

  if (at > inputSize*8) then
  begin
    //WriteLn('file corruption?');
    Exit(MP1_ERR_DATA_SIZE);
  end;
  SamplesCount := 384 * h.channels;
  SetLength(output, SamplesCount);

  n := 0;
  for i := 0 to 11 do
  begin
    for j := 0 to 31 do
    begin
      for k := 0 to h.channels-1 do
      begin
	output[n] := pcm[i][k][j];
	Inc(n);
      end;
    end;
  end;
  Result := (at div 8);
  //h.BlockSize := Result;
end;


end.
